package org.telegram.bot.service.impl;

import com.ruoyi.common.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotMessageTextCheck;
import org.telegram.bot.service.IBotGroupService;
import org.telegram.bot.service.IForwardFromChatService;
import org.telegram.telegrambots.meta.api.objects.Update;

/**
 * 用户转发消息处理
 * @Author: wyx
 * @Date: 2023/6/10 16:31
 */
@Service
public class IForwardFromChatServiceImpl implements IForwardFromChatService {

    @Resource
    private IBotGroupService groupService;

    @Override
    public void handle(Update update, Long chatId, String botId) {
        //违禁词检测
        //频道标题
        String text = update.getMessage().getForwardFromChat().getTitle();
        //转发的内容标题
        if(StringUtils.isNotEmpty(update.getMessage().getCaption())){
            text += update.getMessage().getCaption();
        }
        //转发的内容文本
        if(StringUtils.isNotEmpty(update.getMessage().getText())){
            text += update.getMessage().getText();
        }
        String nickName = "";
        if(StringUtils.isNotEmpty(update.getMessage().getFrom().getFirstName())){
            nickName += update.getMessage().getFrom().getFirstName();
        }
        
        if(StringUtils.isNotEmpty(update.getMessage().getFrom().getLastName())){
            nickName += update.getMessage().getFrom().getLastName();
        }
        BotMessageTextCheck textCheck = new BotMessageTextCheck()
                .setChatId(chatId)
                .setChatType(update.getMessage().getChat().getType())
                .setBotId(botId)
                .setUserId(update.getMessage().getFrom().getId())
                .setMessageId(update.getMessage().getMessageId())
                .setNickName(nickName)
                .setText(text)
                .setEntities(update.getMessage().getEntities());
        //预览的链接
        if (update.getMessage().getLinkPreviewOptions()!=null){
            textCheck.setLinkPreview(update.getMessage().getLinkPreviewOptions().getUrlField());
        }
        groupService.messageTextCheck(textCheck);
    }
}
